package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.MoneyOpeLog;
import com.tbit.uqbike.object.pojo.statistics.ExportMoneyOpeLog;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.MoneyOpeLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 手工资金操作记录(MoneyOpeLog)表控制层
 *
 * @author jing
 * @since 2020-11-20 13:36:33
 */
@RestController
@RequestMapping("/moneyopelog")
public class MoneyOpeLogController {
    /**
     * 服务对象
     */
    @Resource
    private MoneyOpeLogService moneyOpeLogService;

    /**
     * 通过主键查询单条数据
     *
     * @param accountId 主键
     * @return 单条数据
     */
    @PostMapping("/selectOne")
    public JsonResult selectOne(Integer accountId) {
        return JsonResult.ok(this.moneyOpeLogService.queryById(accountId));
    }

    @PostMapping("/queryPage")
    public JsonResult QueryPage(MoneyOpeLog moneyOpeLog) {
        Assert.notNull(moneyOpeLog.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(moneyOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(moneyOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        moneyOpeLog.setPage(new Page(moneyOpeLog.getPageNO(), moneyOpeLog.getRowCount()));
        PageHelperUtil.initPage(moneyOpeLog.getPage());
        List<MoneyOpeLog> moneyOpeLogs = moneyOpeLogService.queryByMoneyOpeLog(moneyOpeLog);
        PageInfo info = new PageInfo(moneyOpeLogs);
        return JsonResult.ok(info.getList(), (int) info.getTotal());
    }

    @GetMapping("/export")
    public void export(MoneyOpeLog moneyOpeLog, HttpServletResponse response) {
        Assert.notNull(moneyOpeLog.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(moneyOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(moneyOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<ExportMoneyOpeLog> exports = moneyOpeLogService.export(moneyOpeLog);
        ExportExcelWrapper<ExportMoneyOpeLog> util = new ExportExcelWrapper<>();
        String filename = "资金修改记录" + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {"操作者名称", "操作者号码", "用户名", "用户号码", "资金类型", "变动类型", "金额", "备注", "操作时间"};
        util.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

}